Data Structures
Tuples and Lists are data structures that hold several values. These structures are important because it allows programmers the ability to store, move, and do operations on many pieces of data at once. Here we go over four main data sturctures used in Python: Lists, Tuples, Sets, and Dictionaries. As a novice programmer, most, if not all, of the tasks you would like to accomplish using a data sturcture can be done purely using lists as a data structure. However, we briefly go over tuples, sets, and dictionaries in order to help familiarize yourself with them in case they are needed or if you have to read another programmer's code that uses these structures. Much of this information comes from and can be found more in depth on the official python documentation. Lists From the official Python documentation : Python knows a number of 'compound 'data types, used to group together other values. The most versatile is the 'list, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.'' Therefore, a list can contain any of the variable types you learned about earlier: a = "John Smith", 5.0, True Lists are '''mutable. This means that you can access any piece of data in the list and change it. Lists provide an index syntax to allow you to both access and change the data stored within it. These indices are 0-indexed. This means that to access the first element, you must access it at position 0, then add one for each element afterwards. There are several ways to access and change an array (using the array above): Standard array access syntax: a0 #returns 10 a2 #returns 5.0 a-1 #returns True. This syntax means "return the first from the end" List slicing: a1: #Returns Smith", 5.0, True i.e. all elements from index 1 and after a:2 #Returns "John Smith", 5.0 i.e. all elements from index 2 and before a1:2 #Returns Smith", 5.0 i.e. all elements from index 1 to index 2 a:-2 #Returns "John Smith" i.e. all elements before the last two Concatenating two lists: a = 2, 3 b = "Joan" c = a + b # c = 2, 3, "James", "Joan" Built-in Functions: Like other variables, lists also utilize built-in functions. For example, the len() function, which can be used to calculate the length of a string, also returns the item count of a list when a list is passed to it: my_list = 8, 3, 3, 5 len(my_list) #returns 5 # Deletes the first value in the array del my_list0 #my_list now equals 3, 3, 5 There are also functions that you call directly on a list in order to manipulate it: my_list = 3, 5 list.append("Another Item") #Changes my_list to 3, 5, "Another Item" For more on functions you can perform on a list, refer to the official Python documentation on lists . Tuples The main use of tuples is for passing a group of ordered data around between functions (which you'll see later) and variables. A tuple is assigned using parentheses: # an empty tuple an_empty_tuple = () person_data = ("Johnny", 23, "24 Address Street") An exception is for tuples of size 1, which are assigned using a comma after the variable small_tuple = "John", Here's how you would extract variables from a tuple: person_data = ("Johnny", 23, "24 Address Street") name, age, address = person_data print address # Prints 24 Address Street Many of the functions that work on a list also work on a tuple, such as len(), concatenation using the + sign, accessing by index, and slicing. However, the main difference of a tuple that sets it apart from a list is that it is immutable. Therefore, you can not delete or add to the tuple after it has been assigned: my_tuple = (1, "Jack", 42) my_tuple.append(5) # Not allowed! del my_tuple1 # Not allowed either! a = my_tuple1 # This is ok because you are not changing my_tuple itself You can always reassign the my_tuple variable and use concatenation to achieve the result. However, if you need to add to a tuple then you should probably just be using a list instead! Sets The main purpose of a set in Python is to maintain an unordered list of unique data. Sets are also more mathematical in nature, supporting operations such as union, intersection, and difference. Sets, like tuples, are immutable. However, because sets are unordered, you can not use operations like accessing a value by index or slicing. Here's the example from the official Python documentation: >>> basket = 'orange', 'apple', 'pear', 'orange', 'banana' >>> fruit = set(basket) # create a set without duplicates >>> fruit set('pear', 'apple', 'banana') >>> 'orange' in fruit # fast membership testing True >>> 'crabgrass' in fruit False >>> # Demonstrate set operations on unique letters from two words ... >>> a = set('abracadabra') >>> b = set('alacazam') >>> a # unique letters in a set('r', 'b', 'c', 'd') >>> a - b # letters in a but not in b set('d', 'b') >>> a | b # letters in either a or b set('c', 'r', 'd', 'b', 'm', 'z', 'l') >>> a & b # letters in both a and b set('c') >>> a ^ b # letters in a or b but not both set('d', 'b', 'm', 'z', 'l') Dictionaries Dictionaries, also known in other programming languages as hashes or associative arrays, are extremely useful for keeping track of key-value pairs. Instead of accessing a value through an index, like in a list and tuple, dictionaries use a key. This is an extremely powerful tool that is very common in practical computer applications. To initialize a dictionary, the syntax is similar to a list, except it curly braces -- {} -- are used to surround key-value pairs denoted by key:value For example: person_data = { "name": "Johhny", "age": 23, "address": "24 Address Street" } Now, what a programmer is accessing in a dictionary is clear and readable when the code is reused: person_data"name" By quickly glancing over the code, you can easily see that the name of the person is being accessed as opposed to code that looks like: person_data0 where you know that it is a person's data, but what part of it? Adding to a dictionary is easy and also helps make code readable: person_data"gender" = "male" Even if that key was never used before in the dictionary, it is now initialized. If there was data already in the dictionary using the key "gender" then it is overwritten by your new value.